﻿using CDSysLDManageProject.BYDTableAdapters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CDSysLDManageProject
{
    public partial class FormEndCodeSetting : Form
    {
        rt_EndCodeInfoTableAdapter endcodeinfo = new rt_EndCodeInfoTableAdapter();
        ServTimeGetTableAdapter sertime = new ServTimeGetTableAdapter();
        DataTable dtendcode = new DataTable();
       
        public FormEndCodeSetting()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            dgvprocess.AutoGenerateColumns = false;
            dtendcode.Columns.Add("kpendcode");
            dtendcode.Columns.Add("secendcode");
            dtendcode.Columns.Add("keyid");
            dgvprocess.AllowUserToAddRows = false;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                dtendcode.Rows.Add(0);
            }
            catch (Exception)
            {

                MessageBox.Show("请先完成上一行的编辑,并保存!");
            }
          
        }

        private void btdelete_Click(object sender, EventArgs e)
        {
            StaticArg.CheckStatus = "open";
            FormClosedCheck cc = new FormClosedCheck();
            cc.ShowDialog();

            if (StaticArg.CheckStatus == "close")
            {

                if (dgvprocess.SelectedRows.Count >= 1)
                {
                    for (int i = 0; i < dgvprocess.SelectedRows.Count; i++)
                    {

                        int kid = int.Parse(dgvprocess.SelectedRows[i].Cells["keyid"].Value.ToString());
                        try
                        {
                            endcodeinfo.DeleteByKid(kid);
                        }
                        catch (Exception ex)
                        {

                            Log.SystemError.Add("删除电池结束状态失败，原因是:" + ex.ToString());
                            MessageBox.Show("删除电池结束状态失败!");
                        }
                    }
                    FormEndCodeSetting_Load(null, null);
                }
                else
                {
                    MessageBox.Show("请选择一条记录!");
                }
            }
            else
            {
            }
            
        }

        private void btsave_Click(object sender, EventArgs e)
        {
            StaticArg.CheckStatus = "open";
            FormClosedCheck cc = new FormClosedCheck();
            cc.ShowDialog();
            //验证通过了才可以修改
            if (StaticArg.CheckStatus == "close")
            {
                for (int i = 0; i < dgvprocess.RowCount; i++)
                {

                    if (dgvprocess.Rows[i].Cells["kpendcode"].Value == null && dgvprocess.Rows[i].Cells["secendcode"].Value == null)
                    {
                        MessageBox.Show("请输入完整的电池充放电结束状态信息!");
                        continue;
                    }
                    if (dgvprocess.Rows[i].Cells["kpendcode"].Value.ToString() == "" || dgvprocess.Rows[i].Cells["secendcode"].Value.ToString() == "" )
                    {
                        MessageBox.Show("请输入完整的电池充放电结束状态信息!");
                        return;
                    }

                    //不存在KID则insert
                    if (dgvprocess.Rows[i].Cells["keyid"].Value.ToString() == "0")
                    {
                        DateTime dt = DateTime.Parse(sertime.ScalarQuery().ToString());
                        try
                        {
                            endcodeinfo.InsertEndCode(dgvprocess.Rows[i].Cells["kpendcode"].Value.ToString().Replace(" ",""), dgvprocess.Rows[i].Cells["secendcode"].Value.ToString(), DateTime.Parse(sertime.ScalarQuery().ToString()), null, null);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("将电池充放电结束状态存到数据失败,请输入正确的参数!");
                            Log.SystemError.Add("将电池充放电结束状态存到数据失败,请输入正确的参数!" + ex.ToString());
                        }

                    }//否则看是否修改
                    else
                    {
                        int ProcessCount = int.Parse(endcodeinfo.GetEndCodeCountByKPSECCode(dgvprocess.Rows[i].Cells["kpendcode"].Value.ToString(), dgvprocess.Rows[i].Cells["secendcode"].Value.ToString()).ToString());
                        if (ProcessCount > 0)
                        {

                        }
                        else
                        {

                            DateTime dt = DateTime.Parse(sertime.ScalarQuery().ToString());
                            try
                            {
                                endcodeinfo.UpdateEndCodeByKid(dgvprocess.Rows[i].Cells["kpendcode"].Value.ToString().Replace(" ", ""), dgvprocess.Rows[i].Cells["secendcode"].Value.ToString(), DateTime.Parse(sertime.ScalarQuery().ToString()), int.Parse(dgvprocess.Rows[i].Cells["keyid"].Value.ToString()));
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("将电池充放电结束状态同步到数据失败,请输入正确的参数!");
                                Log.SystemError.Add("将电池充放电结束状态同步到数据失败,请输入正确的参数!" + ex.ToString());
                            }


                        }
                    }

                }
                FormEndCodeSetting_Load(null, null);
                
            }
            else
            {

            }
            
        }

        private void FormEndCodeSetting_Load(object sender, EventArgs e)
        {
            dtendcode = endcodeinfo.GetEndCodeInfo();

            dgvprocess.DataSource = dtendcode;
        }
    }
}
